﻿using AttachScreenShotToJira.CODE.CORE_LOGIC;
using AttachScreenShotToJira.CODE.GENERAL;
using AttachScreenShotToJira.FORMS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AttachScreenShotToJira
{
    public partial class frmMain : Form
    {
        public string g_Filename;

        public frmMain(string filename)
        {
            InitializeComponent();

            g_Filename = filename;
        }

        private void mmiAbout_Click(object sender, EventArgs e)
        {
            frmAbout frmtoShow = new frmAbout();
            frmtoShow.ShowDialog();
        }

        private void mmiClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void manageSettingsVisual()
        {
            MessageBox.Show("You need to provide the JIRA settings first.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //display settings
            frmSettings frmStt = new frmSettings();
            frmStt.ShowDialog();
        }

        private void btnAttach_Click(object sender, EventArgs e)
        {
            attachFile(edxJiraNumber.Text);
        }   

        private void attachFile(string ticketnumber)
        {
            if (SETTINGS.areSettingsValid())
            {
                if (edxJiraNumber.Text != "")
                {
                    //proceed to upload
                    string msg = JIRA_UPLOAD.AddAttachment(ticketnumber, g_Filename);

                    if (msg == "")
                    {
                        //reset
                        g_Filename = "";
                        edxJiraNumber.Text = "";

                        if (!Properties.Settings.Default.bConfirmUpload)
                        {
                            frmConfirm confirm = new frmConfirm();
                            confirm.ShowDialog();
                        }

                        Close();
                    }
                    else
                    {
                        MessageBox.Show("Unable to upload Screenshot. Details: "+msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("You need to provide a Jira Ticket number.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    edxJiraNumber.Focus();
                }
            }
            else
            {
                //settings are not valid
                manageSettingsVisual();
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            if (!SETTINGS.areSettingsValid())
            {
                //settings are not valid
                manageSettingsVisual();
            }

            //set greenshot status
            smtGreenShot.Checked = GREENSHOT_HANDLING.AlreadyInGreenshot();

            if (g_Filename == "")
            {
                edxJiraNumber.Enabled = false;
                btnAttach.Enabled = false;
            }
            else
            {
                edxJiraNumber.Enabled = true;
                btnAttach.Enabled = true;
            }
        }

        private void smtSettings_Click(object sender, EventArgs e)
        {
            frmSettings frmStt = new frmSettings();
            frmStt.ShowDialog();
        }

        private void smtGreenShot_Click(object sender, EventArgs e)
        {
            //add remove from greenshot
            GREENSHOT_HANDLING.handleStatusinGreenShot(!smtGreenShot.Checked, Properties.Settings.Default.sGreenshotINI);

            //set visual
            smtGreenShot.Checked = !smtGreenShot.Checked;
        }

        private void edxJiraNumber_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                attachFile(edxJiraNumber.Text);
            }
        }
    }
}
